from .get_html import get_html
from .type import Album
from .utils import parse_json, remove_brackets


def parse_meta(html):
    meta = html.select_one('meta[itemprop="page_data"]')
    obj = parse_json(meta["content"])
    album = obj.album

    tracks = {}
    for track in obj.album_appearances:
        number = track.track_number
        url = track.song.url
        tracks[url] = number

    return Album(
        artist=remove_brackets(album.artist.name),
        cover=album.cover_art_url,
        id=album.api_path,
        release=album.release_date,
        title=remove_brackets(album.name),
        url=album.url,
        track_urls=tracks
    )


async def get_album(url: str) -> Album:
    html = await get_html(url)
    return parse_meta(html)
